using System;
using System.IO;
using NVelocity.App;
using NVelocity.Runtime;

using Commons.Collections;

namespace NVelocity.Test {
	
    /// <summary>
    /// Test suite for Templates.
    /// </summary>
    /// <author> <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    /// </author>
    /// <author> <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    /// </author>
    /// <author> <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
    /// </author>
    /// <author> <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    /// </author>
    /// <version>$Id: TemplateTestSuite.cs,v 1.2 2002/07/30 07:26:51 corts Exp $</version>
    public class TemplateTestSuite:NUnit.Framework.TestSuite, TemplateTestBase {

	private ExtendedProperties testProperties;
		
	/// <summary>
	/// Creates an instace of the Apache Velocity test suite.
	/// </summary>
	public TemplateTestSuite() {
	    try {
		Velocity.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, NVelocity.Test.TemplateTestBase_Fields.FILE_RESOURCE_LOADER_PATH);
				
		Velocity.SetProperty(RuntimeConstants_Fields.RUNTIME_LOG_ERROR_STACKTRACE, "true");
		Velocity.SetProperty(RuntimeConstants_Fields.RUNTIME_LOG_WARN_STACKTRACE, "true");
		Velocity.SetProperty(RuntimeConstants_Fields.RUNTIME_LOG_INFO_STACKTRACE, "true");
				
		Velocity.Init();
				
		testProperties = new ExtendedProperties();
		testProperties.Load(new FileStream(TemplateTestBase_Fields.TEST_CASE_PROPERTIES, FileMode.Open, FileAccess.Read));
	    }
	    catch (System.Exception e) {
		throw new System.Exception("Cannot setup TemplateTestSuite!");
	    }
			
	    addTemplateTestCases();
	}
		
	/// <summary>
	/// Adds the template test cases to run to this test suite.  Template test
	/// cases are listed in the <code>TEST_CASE_PROPERTIES</code> file.
	/// </summary>
	private void  addTemplateTestCases() {
	    System.String template;
	    for (int i = 1; ; i++) {
		template = (System.String) testProperties.GetString(getTemplateTestKey(i));
				
		if (template != null) {
		    System.Console.Out.WriteLine("Adding TemplateTestCase : " + template);
		    AddTest(new TemplateTestCase(template));
		} else {
		    // Assume we're done adding template test cases.
		    break;
		}
	    }
	}
		
	/// <summary>
	/// Macro which returns the properties file key for the specified template
	/// test number.
	/// </summary>
	/// <param name="nbr">The template test number to return a property key for.</param>
	/// <returns>The property key.</returns>
	private static System.String getTemplateTestKey(int nbr) {
	    return ("test.template." + nbr);
	}
    }
}